<?php

	class Menu {

		var $menus;
		
		function Menu()
		{
			$this->obj =& get_instance();
		}
		
		function create_menu(){
			$name = $this->obj->session->userdata("usr_name");
			$query = $this->obj->db->query("SELECT a.kd_mod,a.nm_mod,a.controller,a.refer,a.icon_class
											FROM mod_app a
												INNER JOIN grp_role b
													ON a.kd_mod=b.kd_mod
													AND a.refer='#'
												INNER JOIN usr_login c
													ON b.kd_group=c.kd_group
											WHERE c.usr_name='$name'");
			
			$this->menus = "<ul><li><a href='".base_url()."home'><span class='icon16 icomoon-icon-stats-up'></span>Dashboard</a></li>";
			foreach ($query->result() as $row){
				$this->menus .= "<li><a href='#'><span class='".$row->icon_class."'></span>".$row->nm_mod."</a>";
				$query2 = $this->obj->db->query("SELECT a.nm_mod,a.controller,a.refer,a.icon_class
											FROM mod_app a
												INNER JOIN grp_role b
													ON a.kd_mod=b.kd_mod
													AND a.refer=".$row->kd_mod."
												INNER JOIN usr_login c
													ON b.kd_group=c.kd_group
											WHERE c.usr_name='$name'");
				$this->menus .= "<ul class='sub'>";
				foreach ($query2->result() as $row2){
					$this->menus .= "<li><a href='".base_url().$row2->controller."'><span class='".$row2->icon_class."'></span>".$row2->nm_mod."</a></li>";
				}
				$this->menus .= "</ul></li>";
			}
			$this->menus .= "</ul>";

		}
		
		function get_menu(){
			return $this->menus;
		}
		function create_sitemap($sub_id){
			$this->sitemap.="<ul class='sitemap'>";
			if($this->obj->session->userdata('level') == "super administrator"){
				$query = $this->obj->db-> query("SELECT c.* FROM app_menus AS a,app_files as c WHERE a.file_id=c.id AND a.`position`=1 AND a.sub_id='".$sub_id."' AND c.lang='".$_SESSION['lang']."' GROUP BY a.position,a.id ORDER BY sort");
			}
			elseif($this->obj->session->userdata('level') != ""){
				$query = $this->obj->db-> query("SELECT c.* FROM app_menus AS a,app_users_access AS b,app_files as c WHERE a.file_id=c.id AND a.file_id=b.file_id AND b.doshow=1 AND b.level_id='".$this->obj->session->userdata('level')."' AND a.`position`=1 AND a.sub_id='".$sub_id."' AND c.lang='".$_SESSION['lang']."' GROUP BY a.position,a.id ORDER BY sort");
			}else{
				$query = $this->obj->db-> query("SELECT c.* FROM app_menus AS a,app_users_access AS b,app_files as c WHERE a.file_id=c.id AND a.file_id=b.file_id AND b.doshow=1 AND b.level_id='guest' AND a.`position`=1 AND a.sub_id='".$sub_id."' AND c.lang='".$_SESSION['lang']."' GROUP BY a.position,a.id ORDER BY sort");
			}

			foreach ($query->result() as $row){
				if($row->module == "#" || $row->module=="" ) $this->sitemap .= "<li><a href=#>".$row->filename."</a>";
				else	$this->sitemap .= "<li>".anchor($row->module,$row->filename);
				$this->create_sitemap($row->id);

				$this->sitemap .= "</li>";
			}
			$this->sitemap.="</ul>";

		}
		
	}	
